package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.entity.vo.CustomerGoodsVo;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @author qingyu
 * @create 2022-01-02 20:58
 * @description
 */
@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {
    @Autowired
    private CustomerReturnListGoodsDao customerReturnListGoodsDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private CustomerReturnListDao customerReturnListDao;

    /**
     * 客户退货单保存 并加库存
     * @param customerReturnList
     * @param customerReturnListGoodsStr
     * @param returnNumber
     */
    @Override
    public void save(CustomerReturnList customerReturnList, String customerReturnListGoodsStr, String returnNumber) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<CustomerReturnList>>() {
        }.getType();
        List<CustomerReturnListGoods> customerReturnListGoodsList = gson.fromJson(customerReturnListGoodsStr, type);
        for (CustomerReturnListGoods customerReturnListGoods : customerReturnListGoodsList){
            String goodsCode = customerReturnListGoods.getGoodsCode();
            Integer integer = goodsDao.getGood(goodsCode);
            Integer goodsNum = customerReturnListGoods.getGoodsNum();
            goodsDao.update(goodsCode,(integer + goodsNum));
            customerReturnListGoodsDao.insert(customerReturnListGoods);
        }
        customerReturnList.setUserId(1);
        customerReturnListDao.save(customerReturnList);
    }

    /**
     * 通过customerReturnListId退货单商品信息
     * @param customerReturnListId
     * @return
     */
    @Override
    public List<CustomerReturnListGoods> goodList(Integer customerReturnListId) {
        List<CustomerReturnListGoods> customerReturnListGoods = customerReturnListGoodsDao.goodList(customerReturnListId);
        return customerReturnListGoods;
    }

    /**
     * 通过customerReturnListId删除退货单
     * @param customerReturnListId
     */
    @Override
    public void delete(Integer customerReturnListId) {
        customerReturnListGoodsDao.delete(customerReturnListId);
    }


    @Override
    public List<CustomerGoodsVo> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<CustomerGoodsVo> goodsVos = customerReturnListGoodsDao.count(sTime,eTime,goodsTypeId,codeOrName);

        return goodsVos;

    }
}
